package com.bootdo.hjh.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.service.DistConfigValueService;
import com.bootdo.hjh.cache.MemberCache;
import com.bootdo.hjh.cache.Node;
import com.bootdo.hjh.domain.TeamEdges;
import com.bootdo.hjh.domain.TeamNodeData;
import com.bootdo.hjh.domain.TreeNode;
import com.bootdo.hjh.domain.TreeNodeJson;
import com.bootdo.hjh.service.BankCardService;
import com.bootdo.hjh.service.PointsService;
import com.bootdo.hjh.vo.GroupInfo;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;

/**
 * Created by qingyong on 2018/3/31.
 */
@Controller
public class TeamController {


	private Logger logger = LoggerFactory.getLogger(TeamController.class);
	@Autowired
	private BankCardService bankCardService;
	@Autowired
    UserService userService;

	@Autowired
	private PointsService pointsService;

	@Autowired
	private MemberCache memberCache;

	@Autowired
	DistConfigValueService distConfigValueService;

	@Log("我的团队--血缘关系图")
    @GetMapping("/relationship")
	public String relationship(Model model,UserDO userDO) {
		Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        if(userDO.getUserId() != null) {
        		userId = userDO.getUserId();
        }
        Node node = memberCache.getNode(userId);
        TreeNode treeNode = new TreeNode();
        setChildren(userId, node,treeNode);
        List treeNodeList = new ArrayList<>();
        treeNodeList.add(treeNode);
        String nodeString = JSON.toJSONString(treeNodeList);
        //setTreeNodeJson(node,treeNodeList);
        //String nodeString = JSON.toJSONString(treeNodeList);
        model.addAttribute("treeNode", nodeString);
//        logger.info("treeNode1 : {}",nodeString);
        return "hjh/relationship";
	}

	private void setChildren(Long startUserId, Node node,TreeNode treeNode) {
		Long superManUserId = distConfigValueService.getCAN_SEE_ALL_LELVE_MEMBER_USER_ID();
		String activeStatus = "";
		if(node.getInfo().getActiveTime() == null)
		{
			activeStatus = "未激活";
		}
        treeNode.setName(node.getInfo().getUsername().concat(" ").concat(node.getInfo().getName()).concat(" ").concat(activeStatus));
        if(node.getRecommendList() != null && !node.getRecommendList().isEmpty()) {
        		List<TreeNode> children = new ArrayList<>();
        		if (node.getInfo().getUserId().equals(startUserId) || superManUserId.equals(startUserId)) {
					for(Long userId : node.getRecommendList()) {
						Node tmpNode = memberCache.getNode(userId);
						TreeNode tmpTreeNode = new TreeNode();
						children.add(tmpTreeNode);
						treeNode.setChildren(children);

						setChildren(startUserId, tmpNode, tmpTreeNode);
					}
				}
        }else {
        		//treeNode.setIsParent(true);
        }
	}

	public void setTreeNodeJson(Node node,List treeNodeList) {
		TreeNodeJson treeNodeJson = new TreeNodeJson();
		treeNodeJson.setId(node.getInfo().getUserId());
		treeNodeJson.setName(node.getInfo().getUsername().concat(" ").concat(node.getInfo().getName()));
		treeNodeJson.setPid(node.getInfo().getUserId());
		treeNodeList.add(treeNodeJson);
        if(node.getRecommendList() != null && !node.getRecommendList().isEmpty()) {
        		for(Long userId : node.getRecommendList()) {
        			Node tmpNode = memberCache.getNode(userId);
        			TreeNodeJson tmpNodeJson = new TreeNodeJson();
        			tmpNodeJson.setId(tmpNode.getInfo().getUserId());
        			tmpNodeJson.setName(tmpNode.getInfo().getUsername().concat(" ").concat(tmpNode.getInfo().getName()));
        			tmpNodeJson.setPid(node.getInfo().getUserId());
        			treeNodeList.add(tmpNodeJson);
        			setTreeNodeJson(tmpNode,treeNodeList);
        		}
        }
	}

	@Log("我的团队--血缘关系图-加载数据")
    @GetMapping("/relationship_json")
	@ResponseBody
	public String relationshipJson(Model model,UserDO userDO) {
		Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Long userId = ((UserDO)obj).getUserId();
        if(userDO.getUserId() != null) {
        		userId = userDO.getUserId();
        }
        return "hjh/relationship";
	}
}
